﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BaGiaDanhCuop
{
    public partial class ShopPage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        // Constructor
        UIElementRendererHelper uiRenderer;

        public static BackgoundMove Backgrounds;

        //background
        Texture2D background_day_1;
        Texture2D background_day_2;
        Texture2D background_day_3;
        Texture2D background_afternoon_1;
        Texture2D background_afternoon_2;
        Texture2D background_afternoon_3;
        Texture2D background_night_1;
        Texture2D background_night_2;
        Texture2D background_night_3;
        Texture2D road, roadnight, cloud;
        Texture2D sky_day,sky_afternoon, sky_night;

        Texture2D coin;

        public ShopPage()
        {
            InitializeComponent();

            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            uiRenderer = new UIElementRendererHelper(this);

            LoadContent();
            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            uiRenderer.Dispose();

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            double delta = e.ElapsedTime.TotalSeconds;

        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your drawing code here
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            uiRenderer.PreRender();
            uiRenderer.Draw();

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            //background
            spriteBatch.Draw(coin, new Vector2(600, 80), Color.White);
            
            spriteBatch.End();
        }

        public void LoadContent()
        {
            //background
            background_afternoon_1 = contentManager.Load<Texture2D>("Asset/afternoon1");
            background_afternoon_2 = contentManager.Load<Texture2D>("Asset/afternoon2");
            background_afternoon_3 = contentManager.Load<Texture2D>("Asset/afternoon3");
            background_day_1 = contentManager.Load<Texture2D>("Asset/day1");
            background_day_2 = contentManager.Load<Texture2D>("Asset/day2");
            background_day_3 = contentManager.Load<Texture2D>("Asset/day3");
            background_night_1 = contentManager.Load<Texture2D>("Asset/night1");
            background_night_2 = contentManager.Load<Texture2D>("Asset/night2");
            background_night_3 = contentManager.Load<Texture2D>("Asset/night3");
            road = contentManager.Load<Texture2D>("Asset/roadday");
            roadnight = contentManager.Load<Texture2D>("Asset/roadnight");
            sky_afternoon = contentManager.Load<Texture2D>("Asset/Sky_Afternoon");
            sky_day = contentManager.Load<Texture2D>("Asset/Sky_Day");
            sky_night = contentManager.Load<Texture2D>("Asset/Sky_Night");
            cloud = contentManager.Load<Texture2D>("Asset/Cloud");

            Backgrounds = new BackgoundMove(0, background_day_1, background_day_2, background_day_3,
                background_afternoon_1, background_afternoon_2, background_afternoon_3, background_night_1, background_night_2, background_night_3, 
                road, roadnight, sky_day, sky_afternoon, sky_night, cloud);

            //--------------------
            coin = contentManager.Load<Texture2D>("Asset/Icon_Coin");
        }


        private void Bt_PlayGameClick(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/HelpPage.xaml", UriKind.Relative));
        }

        private void Bt_ShopClick(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/BuyPage.xaml", UriKind.Relative));
        }

        private void Bt_BackMainClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}